﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Omu.Awesome.Mvc;
using XPressify.Data;
using XPressify.Models;

namespace XPressify.Controllers
{
    public class MembersLookupController : LookupController
    {
        [Dependency]
        public IRepository Repository { get; set; }


        public override ActionResult SearchForm()
        {
            return PartialView();
        }


        [HttpPost]
        public ActionResult Search(string name, IEnumerable<Guid> selected, string currentUserIdString,Guid subjectId)
        {
            var currentUserId = new Guid(currentUserIdString);

            var json = Session["allMembers"] as string;
            var allMembers = JsonHelper.Deserialize<List<FriendsLookupModels>>(json);
            var result = new List<FriendsLookupModels>();
            if (name != "")
            {
                if (selected != null)
                {
                    result = allMembers.Where(
                        usr =>
                        (usr.FirstName.ToLower().StartsWith(name.ToLower()) ||
                            usr.LastName.ToLower().StartsWith(name.ToLower())) && !selected.Contains(usr.Id)).ToList();
                }
                else
                {
                    result = allMembers.Where(
                        usr =>
                        (usr.FirstName.ToLower().StartsWith(name.ToLower()) ||
                            usr.LastName.ToLower().StartsWith(name.ToLower()))).ToList();
                }
            }
            else
            {
                if (selected != null)
                {
                    result = allMembers.Where(usr => !selected.Contains(usr.Id)).
                        ToList();
                }
                else
                {
                    result = allMembers.ToList();
                }
            }
            return View(@"Awesome\LookupList", result);

        }


        public ActionResult Get(Guid id)
        {
            var json = Session["allMembers"] as string;
            var allMembers = JsonHelper.Deserialize<List<FriendsLookupModels>>(json);
            var person = allMembers.SingleOrDefault(x => x.Id == id);
            return person == null ? Content("") : Content(person.Fullname);
        }

        #region needed only if you use multiselect = true
        public ActionResult Selected(IEnumerable<Guid> selected)
        {

            var json = Session["allMembers"] as string;
            var allMembers = JsonHelper.Deserialize<List<FriendsLookupModels>>(json);
            var selectedUsers = allMembers.Where(usr => selected.Contains(usr.Id));
            return View(@"Awesome\LookupList", selectedUsers);
        }

        public ActionResult GetMultiple(IEnumerable<Guid> selected)
        {
            var json = Session["allMembers"] as string;
            var allMembers = JsonHelper.Deserialize<List<FriendsLookupModels>>(json);
            return Json(allMembers.Where(usr => selected.Contains(usr.Id)).Select(x => new { Text = x.FirstName + " " + x.LastName }));
        }
        #endregion

    }
}
